package net.mikoo.seals.boss.model;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import net.mikoo.seals.boss.common.BaseModel;
import net.mikoo.seals.boss.common.utils.DateUtil;

public class Upfile extends BaseModel {

	private static final long serialVersionUID = 7090671103072825893L;

	private Long upfileId;

	/**
	 * 上传人
	 */
	private Long partyId;
	
	private String module;

	/**
	 * 简介
	 */
	private String contents;

	/**
	 * 文件类型
	 */
	private String fileType;

	/**
	 * 文件名称
	 */
	private String fileName;

	/**
	 * 文件大小
	 */
	private Long fileSize =0L;

	/**
	 * 文件地址
	 */
	private String fileUrl;

	/**
	 * 添加ip
	 */
	private String uploadIp;

	/**
	 * 添加时间
	 */
	private Date uploadTime;
	
	private CommonsMultipartFile file;


	public Upfile() {

	}

	public Long getUpfileId() {
		return upfileId;
	}

	public void setUpfileId(Long upfileId) {
		this.upfileId = upfileId;
	}

	public Long getPartyId() {
		return partyId;
	}

	public void setPartyId(Long partyId) {
		this.partyId = partyId;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public String getFileType() {
		
		if(file != null) {
			return file.getContentType();
		}
		
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public String getFileName() {
		
		if(file != null) {
			return file.getOriginalFilename();
		}
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Long getFileSize() {
		
		if(file != null) {
			return file.getSize();
		}
		
		return fileSize;
	}

	public void setFileSize(Long fileSize) {
		
		
		this.fileSize = fileSize;
	}

	public String getFileUrl() {
		
		if(StringUtils.isBlank(fileUrl)) {
			String str = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
			String curr = System.currentTimeMillis()+"";
			fileUrl = "upload/".concat(module+"/").concat(DateUtil.getCurrentStr("yyyyMMdd")).concat("/"+curr+str+"."+getExtName());
		}
		
		return fileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}

	public String getUploadIp() {
		return uploadIp;
	}

	public void setUploadIp(String uploadIp) {
		this.uploadIp = uploadIp;
	}

	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	
	public CommonsMultipartFile getFile() {
		return file;
	}

	public void setFile(CommonsMultipartFile file) {
		this.file = file;
	}

	public String getExtName() {
		
		if(file != null) {
			String orgname = file.getOriginalFilename();
			int start = orgname.indexOf(".");
			return orgname.substring(start+1, orgname.length());
		}
		
		return file.getOriginalFilename();
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}
	
	

}
